package com.rustic.server.controller.admapi;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.xy.base.server.utils.PageUtil;
import com.rustic.server.query.NationalityDictionaryAdminListQuery;
import com.rustic.server.dto.NationalityDictionaryAddDTO;
import com.rustic.server.dto.NationalityDictionaryUpdateDTO;
import com.rustic.server.po.NationalityDictionary;
import com.rustic.server.service.NationalityDictionaryService;
import com.rustic.server.vo.list.NationalityDictionaryAdminListVO;
import com.rustic.server.vo.NationalityDictionaryVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import cn.xy.commons.annotation.OperateLog;
import cn.xy.commons.core.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 表[nationality_dictionary]对应的后台管理类
 *
 * @author auto
 *
 */

@Slf4j
@RestController
@RequestMapping("/admapi/nationalityDictionary")
@Api(tags = {"国籍字典管理接口"})
public class NationalityDictionaryAdminController {

    @Autowired
    private NationalityDictionaryService nationalityDictionaryService;

    @ApiOperation(value = "国籍字典-列表", notes = "权限key: nationality-dictionary-list")
    @GetMapping("/list")
    @PreAuthorize("@validate.hasPermission('nationality-dictionary-list')")
    public Result<Page<NationalityDictionaryAdminListVO>> list(@Validated NationalityDictionaryAdminListQuery query) {
        LambdaQueryWrapper<NationalityDictionary> queryWrapper = nationalityDictionaryService.toAdminListWrapper(query);
        if (queryWrapper == null) {
            return Result.data(new Page<>());
        }
        Page<NationalityDictionary> pageData = nationalityDictionaryService.page(query.toPage(), queryWrapper);
        List<NationalityDictionary> nationalityDictionaryList = pageData.getRecords();
        if (CollUtil.isEmpty(nationalityDictionaryList)) {
            return Result.data(new Page<>());
        }
        List<NationalityDictionaryAdminListVO> results = nationalityDictionaryService.toAdminListVo(nationalityDictionaryList);
        return Result.data(PageUtil.toPage(pageData, results));
    }

    @ApiOperation(value = "国籍字典-详情", notes = "权限key: nationality-dictionary-detail")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "国籍字典id"),
    })
    @GetMapping("/detail")
    @PreAuthorize("@validate.hasPermission('nationality-dictionary-detail')")
    public Result<NationalityDictionaryVO> detail(int id) {
        if (id <= 0) {
            return Result.error(NationalityDictionaryService.NATIONALITY_DICTIONARY_NOT_EXIST);
        }
        NationalityDictionary nationalityDictionary = nationalityDictionaryService.getById(id);
        if (nationalityDictionary == null) {
            return Result.error(NationalityDictionaryService.NATIONALITY_DICTIONARY_NOT_EXIST);
        }
        NationalityDictionaryVO nationalityDictionaryVO = new NationalityDictionaryVO(nationalityDictionary);
        return Result.data(nationalityDictionaryVO);
    }

    @ApiOperation(value = "国籍字典-增加", notes = "权限key: nationality-dictionary-add")
    @PostMapping("/add")
    @PreAuthorize("@validate.hasPermission('nationality-dictionary-add')")
    @OperateLog(description = "增加国籍字典")
    public Result<String> add(@RequestBody @Validated NationalityDictionaryAddDTO nationalityDictionaryAddDTO) {
        NationalityDictionary nationalityDictionary = nationalityDictionaryAddDTO.toNationalityDictionary();
        nationalityDictionaryService.save(nationalityDictionary);
        return Result.ok("添加国籍字典成功");
    }

    @ApiOperation(value = "国籍字典-更新", notes = "权限key: nationality-dictionary-update")
    @PutMapping("/update")
    @PreAuthorize("@validate.hasPermission('nationality-dictionary-update')")
    @OperateLog(description = "更新国籍字典")
    public Result<String> update(@RequestBody @Validated NationalityDictionaryUpdateDTO nationalityDictionaryUpdateDTO) {
        NationalityDictionary nationalityDictionary = nationalityDictionaryService.getById(nationalityDictionaryUpdateDTO.getId());
        if (nationalityDictionary == null) {
            return Result.error(NationalityDictionaryService.NATIONALITY_DICTIONARY_NOT_EXIST);
        }
        BeanUtils.copyProperties(nationalityDictionaryUpdateDTO, nationalityDictionary);
        nationalityDictionaryService.updateById(nationalityDictionary);
        return Result.ok("编辑国籍字典成功");
    }

    @ApiOperation(value = "国籍字典-删除", notes = "权限key: nationality-dictionary-delete")
    @DeleteMapping("/delete")
    @PreAuthorize("@validate.hasPermission('nationality-dictionary-delete')")
    @OperateLog(description = "删除国籍字典")
    public Result<String> delete(int id) {
        if (id <= 0) {
            return Result.error(NationalityDictionaryService.NATIONALITY_DICTIONARY_NOT_EXIST);
        }
        NationalityDictionary nationalityDictionary = nationalityDictionaryService.getById(id);
        if (nationalityDictionary == null) {
            return Result.error(NationalityDictionaryService.NATIONALITY_DICTIONARY_NOT_EXIST);
        }
        nationalityDictionaryService.removeById(id);
        return Result.ok("删除国籍字典成功");
    }
}
